package com.huike.pmps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.pmps.dao.CustomerSuccessDao;
import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.CustomerSuccess;
import com.huike.pmps.model.vo.PageResult;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.model.vo.successteam.CustomerSuccessNode;
import com.huike.pmps.model.vo.successteam.CustomerSuccessTree;
import com.huike.pmps.service.CustomerSuccessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author：张春旺(010101)
 * date: 2021-06-29
 * WeChat:18311488838
 * QQ:347329812
 */
@Service
public class CustomerSuccessServiceImpl implements CustomerSuccessService {

    @Autowired
    CustomerSuccessDao customerSuccessDao;

    @Override
    public Result selectByCondition(PageInfo pageInfo, String teamName, String aliasName, String charge) {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "team_name", "alias_name", "parent_id", "charge", "description");
        queryWrapper.eq("parent_id", "0");
        queryWrapper.eq("is_deleted", "0");
        if (StringUtils.isNotBlank(teamName)) {
            queryWrapper.eq("team_name", teamName);
        }
        if (StringUtils.isNotBlank(aliasName)) {
            queryWrapper.eq("alias_name", aliasName);
        }
        if (StringUtils.isNotBlank(charge)) {
            queryWrapper.eq("charge", charge);
        }

        Page<CustomerSuccess> page = new Page<>(pageInfo.getPage(), pageInfo.getPageSize());
        Page<CustomerSuccess> pageList = customerSuccessDao.selectPage(page, queryWrapper);
        if (pageList.getSize() > 0) {
            PageResult pageResult = PageResult.builder().pageNumber(pageList.getCurrent()).pageSize(pageList.getSize())
                    .total(pageList.getTotal()).rows(pageList.getRecords()).build();
            return Result.success(pageResult);
        } else {
            return Result.success("没有符合条件的记录");
        }
    }

    @Override
    public Result selectAllCustomerSuccess() {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", "0");
        queryWrapper.eq("is_deleted", "0");
        queryWrapper.select("id", "team_name", "charge");
        return Result.success(customerSuccessDao.selectList(queryWrapper));
    }

    @Override
    public Result insertOne(CustomerSuccess customerSuccess) {
        customerSuccess.setCreateTime(new Date());
        customerSuccess.setIsDeleted(Byte.valueOf("0"));
        //顶层成功客户部门
        if (customerSuccess.getParentId() == null) {
            customerSuccess.setParentId(0);
            List<CustomerSuccess> customerSuccesses = selectByCustomerSuccessName(customerSuccess);
            if (customerSuccesses.size() > 0){
                return Result.failure("成功客户名/别名重复，请勿重复添加");
            }
        }
        customerSuccessDao.insert(customerSuccess);
        return Result.success();
    }

    @Override
    public Result deleteById(Integer id) {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        queryWrapper.eq("is_deleted", "0");
        if (customerSuccessDao.selectCount(queryWrapper) > 0) {
            return Result.failure("该成功客户团队下还有子团队，请先删除子团队");
        } else {
            customerSuccessDao.delByID(id);
            return Result.success();
        }
    }

    @Override
    public Result updateById(CustomerSuccess customerSuccess) {
        customerSuccessDao.updateById(customerSuccess);
//        if (customerSuccess.getParentId() == 0)
//            customerSuccessDao.updateDependent(customerSuccess.getId(), customerSuccess.getTeamName());
        return Result.success();
    }

    @Override
    public Result selectByParentId(Integer parentId) {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.eq("is_deleted","0");
        queryWrapper.select("id", "team_name","alias_name","parent_id", "charge", "description");
        return Result.success(customerSuccessDao.selectList(queryWrapper));
    }

    private List<CustomerSuccess> selectByCustomerSuccessName(CustomerSuccess customerSuccess) {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("team_name", customerSuccess.getTeamName()).or().like("alias_name", customerSuccess.getAliasName());
        List<CustomerSuccess> CustomerSuccesses = customerSuccessDao.selectList(queryWrapper);
        return CustomerSuccesses;
    }


    @Override
    public Result selectByTree() {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", "0");
        queryWrapper.eq("is_deleted", "0");
        queryWrapper.select("id", "team_name", "charge");
        List<CustomerSuccess> list = customerSuccessDao.selectList(queryWrapper);
        List<CustomerSuccessTree> treeList = new ArrayList<>();
        for(CustomerSuccess cs : list){
            CustomerSuccessTree tree = new CustomerSuccessTree();
            tree.setId(cs.getId());
            tree.setLabel(cs.getTeamName());
            List<CustomerSuccessNode> nodes = selectChildrenByParentId(cs.getId());
            if(nodes != null){
                tree.setChildren(nodes);
            }
            treeList.add(tree);
        }
        return Result.success(treeList);
    }

    private List<CustomerSuccessNode> selectChildrenByParentId(Integer parentId) {
        QueryWrapper<CustomerSuccess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.eq("is_deleted","0");
        queryWrapper.select("id", "team_name");
        List<CustomerSuccess> css = customerSuccessDao.selectList(queryWrapper);
        if(css == null || css.size() == 0 ){
            return null;
        }
        List<CustomerSuccessNode> nodes = new ArrayList<>();
        for(CustomerSuccess cs : css){
            CustomerSuccessNode node = new CustomerSuccessNode();
            node.setId(cs.getId());
            node.setLabel(cs.getTeamName());
            nodes.add(node);
        }
        return nodes;
    }
}
